#!/bin/bash
# Live CD filesystem mounting			-*- shell-script -*-

#Function for parsing command line options with "=" in them
# get_opt("init=/sbin/init") will return "/sbin/init"
get_opt() {
    echo "$@" | cut -d "=" -f 2
}

mountroot ()
{
    CFG_FILE=/etc/moblin-initramfs.cfg
    QUIET="$(grep "quiet" /proc/cmdline)"

    if [ -f ${CFG_FILE} ]
    then
        . ${CFG_FILE}
    else
        if [ "$QUIET" == "" ]; then
                echo "Did not find config file: ${CFG_FILE}"
        fi
        sleep 5
        halt
    fi


    CMDLINE=$(cat /proc/cmdline)

    #Process command line options
    XBMC_PARAMS=""
    for i in ${CMDLINE}; do
        case "${i}" in
            xbmc\=*)
                XBMC_PARAMS=$(get_opt $i)
                ;;
        esac
    done

    AUTOGPU="$( echo $XBMC_PARAMS | grep "autogpu" )"

    if [ "$AUTOGPU" != "" ]; then
        # Needs lspci in initramfs, not yet there
        if [ "$QUIET" = "" ]; then
             echo "found auto"
        fi
        XBMC_AMD="$(lspci -nn | grep 0300 | grep 1002)"
        XBMC_NVIDIA="$(lspci -nn | grep 0300 | grep 10de)"
    else
        XBMC_NVIDIA="$( echo $XBMC_PARAMS | grep "nvidia" )"
        XBMC_AMD="$( echo $XBMC_PARAMS | grep "amd" )"
    fi

    BOOTMEDIAMNTPOINT=/bootmediamnt
    mkdir -p $BOOTMEDIAMNTPOINT
    mkdir -p /squashmnt1
    mkdir -p /squashmnt2
    mkdir -p /persistmnt

    if [ "$ROOT" != "" ]; then
        if [ "$QUIET" == "" ]; then
	        echo "will mount root from ${ROOT}"
	fi

        mount -o ro -t iso9660 ${ROOT} $BOOTMEDIAMNTPOINT 2> /dev/null
        while [ ! -e $BOOTMEDIAMNTPOINT/rootfs.img ]; do
            /bin/sleep 1
            if [ "$QUIET" == "" ]; then
                    echo "Trying again $ROOT ...."
            fi
            mount -o ro ${ROOT} -t iso9660 $BOOTMEDIAMNTPOINT 2> /dev/null
        done
    else
        # Find the CD drive
	found="no"
	while true
	do
		for device in 'cdrom' 'scd0' 'scd1' 'scd2' 'scd3'; do
			if [ "$QUIET" == "" ]; then
				echo "Checking device /dev/${device} for installation source..."
			fi
			if [ -b /dev/${device} ]; then
				mount -o ro -t iso9660 /dev/${device} $BOOTMEDIAMNTPOINT 2> /dev/null
				if [ -e $BOOTMEDIAMNTPOINT/rootfs.img ] ; then
					if [ "$QUIET" == "" ]; then
						echo "Found CD Boot Drive at /dev/${device}"
					fi
					found="yes"
				fi
				umount $BOOTMEDIAMNTPOINT  2> /dev/null
				if [ "$found" = "yes" ]; then
					break;
				fi
				if [ "$QUIET" == "" ]; then
					echo "/dev/${device} does not contain a rootfs"
				fi
			fi
		done
		if [ "$found" = "yes" ]; then
			break;
		fi
		/bin/sleep 5
	done
	if [ "$QUIET" == "" ]; then
        	echo "will mount root from /dev/${device}"
	fi

        mount -o ro -t iso9660 /dev/${device} $BOOTMEDIAMNTPOINT 2> /dev/null

        while [ ! -e $BOOTMEDIAMNTPOINT/rootfs.img ]; do
            /bin/sleep 1
            if [ "$QUIET" == "" ]; then
                echo "Trying again /dev/${device} ...."
            fi
	    mount -o ro -t iso9660 /dev/${device} $BOOTMEDIAMNTPOINT 2> /dev/null
        done
    fi

    XBMC_BOOTTORAM="$( echo $XBMC_PARAMS | grep "boottoram" )"

    if [ "$XBMC_BOOTTORAM" != "" ]; then
        if [ "$QUIET" == "" ]; then
            echo "Copying boot media to RAM ...."
        fi

	# TODO calc size from boot media
	mkdir /bootmediamntRAM
	mount -t tmpfs -o size=500M none /bootmediamntRAM
	cp -R $BOOTMEDIAMNTPOINT/* /bootmediamntRAM 2> /dev/null
	umount $BOOTMEDIAMNTPOINT
	$BOOTMEDIAMNTPOINT=/bootmediamntRAM
    fi

    mount -o ro,loop -t squashfs $BOOTMEDIAMNTPOINT/rootfs.img /squashmnt1

    if [ "$XBMC_NVIDIA" != "" ]; then
        if [ -f $BOOTMEDIAMNTPOINT/restrictedDrivers.nvidia.img ]; then
               if [ "$QUIET" = "" ]; then
                   echo "Mounting NVIDIA drivers..."
               fi
               mount -o ro,loop,noatime,nodiratime $BOOTMEDIAMNTPOINT/restrictedDrivers.nvidia.img /squashmnt2
        fi
    else
        if [ "$XBMC_AMD" != "" ]; then
            if [ -f $BOOTMEDIAMNTPOINT/restrictedDrivers.amd.img ]; then
                if [ "$QUIET" = "" ]; then
                    echo "Mounting AMD drivers..."
                fi
                mount -o ro,loop,noatime,nodiratime $BOOTMEDIAMNTPOINT/restrictedDrivers.amd.img /squashmnt2
            fi
        else
            mount -t tmpfs -o noatime,nodiratime none /squashmnt2

            if [ "$QUIET" = "" ]; then
                echo "Defaulting to Xorg autodetect..."
            fi
        fi
    fi

    mount -t tmpfs -o noatime,nodiratime none /persistmnt

    mount -t unionfs -o dirs=/persistmnt=rw:/squashmnt2=ro:/squashmnt1=ro none ${rootmnt}

    # Correct the permissions of /:
    chmod 755 "${rootmnt}"

    # Make sure the individual ro and rw mounts are accessible from within the root
    # once the union is assumed as /.  This makes it possible to access the
    # component filesystems individually.
    if [ ! -e "${rootmnt}/.bootMedia" ]; then
	  mkdir "${rootmnt}/.bootMedia" 
    fi
    #mkdir "${rootmnt}/.ro1" "${rootmnt}/.ro2" "${rootmnt}/.rw"

    mount --bind $BOOTMEDIAMNTPOINT  "${rootmnt}/.bootMedia"
    #mount --bind /squashmnt1 "${rootmnt}/.ro1"
    #mount --bind /squashmnt2 "${rootmnt}/.ro2"
    #mount --bind /persistmnt "${rootmnt}/.rw"

    if [ -f $BOOTMEDIAMNTPOINT/config/fstab ]; then
        cp $BOOTMEDIAMNTPOINT/config/fstab ${rootmnt}/etc/fstab
#	rm $BOOTMEDIAMNTPOINT/config/fstab
    fi
    if [ -f $BOOTMEDIAMNTPOINT/config/shadow ]; then
        cp $BOOTMEDIAMNTPOINT/config/shadow ${rootmnt}/etc
#	rm $BOOTMEDIAMNTPOINT/config/fstab
	chmod 640 ${rootmnt}/etc/shadow
    fi

    if [ -f $BOOTMEDIAMNTPOINT/config/rc.local ]; then
        cp $BOOTMEDIAMNTPOINT/config/rc.local ${rootmnt}/etc
    fi

    if [ -f $BOOTMEDIAMNTPOINT/config/xorg.conf ]; then
	cp $BOOTMEDIAMNTPOINT/config/xorg.conf ${rootmnt}/etc/X11
    fi

    if [ -f $BOOTMEDIAMNTPOINT/config/lircd.conf ]; then
        cp $BOOTMEDIAMNTPOINT/config/lircd.conf ${rootmnt}/etc/lirc
    fi

    if [ -f $BOOTMEDIAMNTPOINT/config/hardware.conf ]; then
        cp $BOOTMEDIAMNTPOINT/config/hardware.conf ${rootmnt}/etc/lirc
    fi

    if [ -f $BOOTMEDIAMNTPOINT/config/interfaces ]; then
	cp $BOOTMEDIAMNTPOINT/config/interfaces ${rootmnt}/etc/network
    fi
}
